package com.xbongbong.saas.enums.dictionary;

import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.saas.enums.OperateTypeEnum;
import lombok.Getter;

import java.util.Objects;

/**
 * 归档状态
 *
 * @author GuoJun.HU
 * @date 2019/3/13 10:57
 * @since V1.0
 */
@Getter
public enum ArchivedEnum {

    // 归档状态
    ARCHIVED(1, "enum.archived.name.archived", "archived"),
    UN_ARCHIVED(2, "enum.archived.name.un_archived", "unArchived"),
    UNKNOWN(404, "enum.archived.name.unknown", "unknown"),
    ;

    private int code;
    private String name;
    private String attr;

    ArchivedEnum(int code, String name, String attr) {
        this.code = code;
        this.name = name;
        this.attr = attr;
    }

    public static ArchivedEnum getByCode(Integer code) {
        for (ArchivedEnum cacheCode : values()) {
            if (Objects.equals(code, cacheCode.getCode())) {
                return cacheCode;
            }
        }
        return UNKNOWN;
    }

    /**
     * 通过code获取操作类型（归档 or 取消归档）
     * @param code
     * @return com.xbongbong.saas.enums.OperateTypeEnum
     * @author zcp
     * @date 2019/7/6 16:24
     * @since v1.0
     * @version v1.0
     */
    public static OperateTypeEnum getOperateByCode(Integer code){
        if (Objects.equals(code, ARCHIVED.code)) {
            return OperateTypeEnum.ARCHIVED;
        } else if (Objects.equals(code, UN_ARCHIVED.code)) {
            return OperateTypeEnum.CANCEL_ARCHIVED;
        }
        return OperateTypeEnum.EDIT;
    }

    public String getName() {
        return I18nMessageUtil.getMessage(name);
    }
}
